package org.kingtop.mybatis.util;

import java.sql.SQLException;

import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.Utils;

/**
 * 从Druid抄录
 * @author 陈金波
 * @date   2018年1月9日 下午10:06:27    
 * @Copyright: 2018 chen jinbo. All rights reserved.
 */
public class JdbcUtils implements JdbcConstants {

	private static Boolean mysql_driver_version_6 = null;

	public static String getDriverClassName(String rawUrl) throws SQLException {
		if (rawUrl == null) {
			return null;
		}

		if (rawUrl.startsWith("jdbc:derby:")) {
			return "org.apache.derby.jdbc.EmbeddedDriver";
		} else if (rawUrl.startsWith("jdbc:mysql:")) {
			if (mysql_driver_version_6 == null) {
				mysql_driver_version_6 = Utils.loadClass("com.mysql.cj.jdbc.Driver") != null;
			}

			if (mysql_driver_version_6) {
				return MYSQL_DRIVER_6;
			} else {
				return MYSQL_DRIVER;
			}
		} else if (rawUrl.startsWith("jdbc:log4jdbc:")) {
			return LOG4JDBC_DRIVER;
		} else if (rawUrl.startsWith("jdbc:mariadb:")) {
			return MARIADB_DRIVER;
		} else if (rawUrl.startsWith("jdbc:oracle:") || rawUrl.startsWith("JDBC:oracle:")) {
			return ORACLE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
			return ALI_ORACLE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:microsoft:")) {
			return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
		} else if (rawUrl.startsWith("jdbc:sqlserver:")) {
			return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		} else if (rawUrl.startsWith("jdbc:sybase:Tds:")) {
			return "com.sybase.jdbc2.jdbc.SybDriver";
		} else if (rawUrl.startsWith("jdbc:jtds:")) {
			return "net.sourceforge.jtds.jdbc.Driver";
		} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
			return "com.alibaba.druid.mock.MockDriver";
		} else if (rawUrl.startsWith("jdbc:postgresql:")) {
			return POSTGRESQL_DRIVER;
		} else if (rawUrl.startsWith("jdbc:edb:")) {
			return ENTERPRISEDB_DRIVER;
		} else if (rawUrl.startsWith("jdbc:odps:")) {
			return ODPS_DRIVER;
		} else if (rawUrl.startsWith("jdbc:hsqldb:")) {
			return "org.hsqldb.jdbcDriver";
		} else if (rawUrl.startsWith("jdbc:db2:")) {
			return DB2_DRIVER;
		} else if (rawUrl.startsWith("jdbc:sqlite:")) {
			return SQLITE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:ingres:")) {
			return "com.ingres.jdbc.IngresDriver";
		} else if (rawUrl.startsWith("jdbc:h2:")) {
			return H2_DRIVER;
		} else if (rawUrl.startsWith("jdbc:mckoi:")) {
			return "com.mckoi.JDBCDriver";
		} else if (rawUrl.startsWith("jdbc:cloudscape:")) {
			return "COM.cloudscape.core.JDBCDriver";
		} else if (rawUrl.startsWith("jdbc:informix-sqli:")) {
			return "com.informix.jdbc.IfxDriver";
		} else if (rawUrl.startsWith("jdbc:timesten:")) {
			return "com.timesten.jdbc.TimesTenDriver";
		} else if (rawUrl.startsWith("jdbc:as400:")) {
			return "com.ibm.as400.access.AS400JDBCDriver";
		} else if (rawUrl.startsWith("jdbc:sapdb:")) {
			return "com.sap.dbtech.jdbc.DriverSapDB";
		} else if (rawUrl.startsWith("jdbc:JSQLConnect:")) {
			return "com.jnetdirect.jsql.JSQLDriver";
		} else if (rawUrl.startsWith("jdbc:JTurbo:")) {
			return "com.newatlanta.jturbo.driver.Driver";
		} else if (rawUrl.startsWith("jdbc:firebirdsql:")) {
			return "org.firebirdsql.jdbc.FBDriver";
		} else if (rawUrl.startsWith("jdbc:interbase:")) {
			return "interbase.interclient.Driver";
		} else if (rawUrl.startsWith("jdbc:pointbase:")) {
			return "com.pointbase.jdbc.jdbcUniversalDriver";
		} else if (rawUrl.startsWith("jdbc:edbc:")) {
			return "ca.edbc.jdbc.EdbcDriver";
		} else if (rawUrl.startsWith("jdbc:mimer:multi1:")) {
			return "com.mimer.jdbc.Driver";
		} else if (rawUrl.startsWith("jdbc:dm:")) {
			return JdbcConstants.DM_DRIVER;
		} else if (rawUrl.startsWith("jdbc:kingbase:")) {
			return JdbcConstants.KINGBASE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:gbase:")) {
			return JdbcConstants.GBASE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:hive:")) {
			return JdbcConstants.HIVE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:hive2:")) {
			return JdbcConstants.HIVE_DRIVER;
		} else if (rawUrl.startsWith("jdbc:phoenix:thin:")) {
			return "org.apache.phoenix.queryserver.client.Driver";
		} else if (rawUrl.startsWith("jdbc:phoenix://")) {
			return JdbcConstants.PHOENIX_DRIVER;
		} else if (rawUrl.startsWith("jdbc:kylin:")) {
			return JdbcConstants.KYLIN_DRIVER;
		} else if (rawUrl.startsWith("jdbc:elastic:")) {
			return JdbcConstants.ELASTIC_SEARCH_DRIVER;
		} else {
			throw new SQLException("unkow jdbc driver : " + rawUrl);
		}
	}

	public static String getDbType(String rawUrl) throws SQLException {
		return getDbType(rawUrl, getDriverClassName(rawUrl));
	}

	public static String getDbType(String rawUrl, String driverClassName) {
		if (rawUrl == null) {
			return null;
		}

		if (rawUrl.startsWith("jdbc:derby:") || rawUrl.startsWith("jdbc:log4jdbc:derby:")) {
			return DERBY;
		} else if (rawUrl.startsWith("jdbc:mysql:") || rawUrl.startsWith("jdbc:cobar:")
				|| rawUrl.startsWith("jdbc:log4jdbc:mysql:")) {
			return MYSQL;
		} else if (rawUrl.startsWith("jdbc:mariadb:")) {
			return MARIADB;
		} else if (rawUrl.startsWith("jdbc:oracle:") || rawUrl.startsWith("jdbc:log4jdbc:oracle:")) {
			return ORACLE;
		} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
			return ALI_ORACLE;
		} else if (rawUrl.startsWith("jdbc:microsoft:") || rawUrl.startsWith("jdbc:log4jdbc:microsoft:")) {
			return SQL_SERVER;
		} else if (rawUrl.startsWith("jdbc:sqlserver:") || rawUrl.startsWith("jdbc:log4jdbc:sqlserver:")) {
			return SQL_SERVER;
		} else if (rawUrl.startsWith("jdbc:sybase:Tds:") || rawUrl.startsWith("jdbc:log4jdbc:sybase:")) {
			return SYBASE;
		} else if (rawUrl.startsWith("jdbc:jtds:") || rawUrl.startsWith("jdbc:log4jdbc:jtds:")) {
			return JTDS;
		} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
			return MOCK;
		} else if (rawUrl.startsWith("jdbc:postgresql:") || rawUrl.startsWith("jdbc:log4jdbc:postgresql:")) {
			return POSTGRESQL;
		} else if (rawUrl.startsWith("jdbc:edb:")) {
			return ENTERPRISEDB;
		} else if (rawUrl.startsWith("jdbc:hsqldb:") || rawUrl.startsWith("jdbc:log4jdbc:hsqldb:")) {
			return HSQL;
		} else if (rawUrl.startsWith("jdbc:odps:")) {
			return ODPS;
		} else if (rawUrl.startsWith("jdbc:db2:")) {
			return DB2;
		} else if (rawUrl.startsWith("jdbc:sqlite:")) {
			return SQLITE;
		} else if (rawUrl.startsWith("jdbc:ingres:")) {
			return "ingres";
		} else if (rawUrl.startsWith("jdbc:h2:") || rawUrl.startsWith("jdbc:log4jdbc:h2:")) {
			return H2;
		} else if (rawUrl.startsWith("jdbc:mckoi:")) {
			return "mckoi";
		} else if (rawUrl.startsWith("jdbc:cloudscape:")) {
			return "cloudscape";
		} else if (rawUrl.startsWith("jdbc:informix-sqli:") || rawUrl.startsWith("jdbc:log4jdbc:informix-sqli:")) {
			return "informix";
		} else if (rawUrl.startsWith("jdbc:timesten:")) {
			return "timesten";
		} else if (rawUrl.startsWith("jdbc:as400:")) {
			return "as400";
		} else if (rawUrl.startsWith("jdbc:sapdb:")) {
			return "sapdb";
		} else if (rawUrl.startsWith("jdbc:JSQLConnect:")) {
			return "JSQLConnect";
		} else if (rawUrl.startsWith("jdbc:JTurbo:")) {
			return "JTurbo";
		} else if (rawUrl.startsWith("jdbc:firebirdsql:")) {
			return "firebirdsql";
		} else if (rawUrl.startsWith("jdbc:interbase:")) {
			return "interbase";
		} else if (rawUrl.startsWith("jdbc:pointbase:")) {
			return "pointbase";
		} else if (rawUrl.startsWith("jdbc:edbc:")) {
			return "edbc";
		} else if (rawUrl.startsWith("jdbc:mimer:multi1:")) {
			return "mimer";
		} else if (rawUrl.startsWith("jdbc:dm:")) {
			return JdbcConstants.DM;
		} else if (rawUrl.startsWith("jdbc:kingbase:")) {
			return JdbcConstants.KINGBASE;
		} else if (rawUrl.startsWith("jdbc:gbase:")) {
			return JdbcConstants.GBASE;
		} else if (rawUrl.startsWith("jdbc:log4jdbc:")) {
			return LOG4JDBC;
		} else if (rawUrl.startsWith("jdbc:hive:")) {
			return HIVE;
		} else if (rawUrl.startsWith("jdbc:hive2:")) {
			return HIVE;
		} else if (rawUrl.startsWith("jdbc:phoenix:")) {
			return PHOENIX;
		} else if (rawUrl.startsWith("jdbc:elastic:")) {
			return ELASTIC_SEARCH;
		} else {
			return null;
		}
	}

}
